home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / tex-k / impatient / index1.icn < prev    next >
Text File  |  1990-08-09  |  3KB  |  90 lines

  1. # This Icon program carries out the first phase of preparing the index for
  2. # TeX for the Impatient.  The input is the .idx file produced by TeX using
  3. # our macros.  The output should be piped through a sort and then to the
  4. # index2 program.
  5. #
  6. # This program was written by Paul Abrahams.
  7. #
  8. # An index entry has the form:
  9. #   term::type::page suffix
  10. # where `term' is the index term, `type' is T, N, or C,
  11. # `page' is either a folio number or *s, where s is a see-string,
  12. # and `suffix' is either empty or P, E, B, PE, or PB.
  13. # P indicates a principal entry, B and E begin and end a page range.
  14.  
  15.  
  16. global printable, specials
  17. record index_item(term, type, pages)
  18. record pgrec(number, pflag)
  19.  
  20. procedure main(a)
  21.     local idx, fn, entry
  22.     fn := a[1]
  23.     printable := &ascii[33:-1]
  24.     specials := string(printable -- (&ucase ++ &lcase ++ &digits))
  25.     every entry := !&input do
  26.         write(key(entry), "@@@", entry)
  27.     write(&errout, "First indexing pass is complete.")
  28.     return
  29. end
  30.  
  31. procedure key(entry)
  32.     local symb, symb1, type, page
  33.     static collate, hi, sortsequence
  34.     initial {
  35.         collate := specials || &ucase || &digits
  36.         sortsequence := printable -- &lcase
  37.         hi := repl(char(128), 26)
  38.         }
  39.     entry ? (symb := tab(find("::")), move(2), type := move(1),
  40.         move(2), page := tab(many('-0123456789*')))
  41.     symb := remove_leading_specials(symb)
  42.     symb1 := map(symb, &lcase, &ucase)
  43.     return map(map_term(symb1, type), collate, sortsequence) ||
  44.         map(symb, &ucase, hi) || char(1) || page_key(page)
  45. end
  46.     
  47. procedure remove_leading_specials(s)
  48. # remove leading period, backslash, or less-than
  49.     local a, b, c, k
  50.     static kills
  51.     initial {kills := '\\.<'}
  52.  
  53. # a one-character special is left unchanged
  54.     c := s[1:upto("//", s) | 0]
  55.     if *c = 1 & any(kills, c) then
  56.         return s
  57.  
  58.     k := 1
  59.     while s ? (a := tab(k), b:= tab(upto(kills)), 
  60.         tab(many(kills)), c := tab(0)) do
  61.         {s := a || b || c; k +:= *b}
  62.     return s
  63. end
  64.  
  65. procedure map_term(t, c)
  66. # t is an index term, c is "N", "T", or "C"
  67. # Encode NTC as char(1), char(2), char(3)
  68. # Replace each // by the NTC code, then follow t by the NTC code and 1
  69.     local code
  70.  
  71.     code := char(find(c, "NTC") | 4)
  72.     while t[find("//", t)+:2] := code
  73.     return t || code || char(1)
  74. end 
  75.  
  76. procedure page_key(p)
  77. # convert p, which may be negative, to a character string key
  78. # Negative numbers must sort with the smallest closest to 0.
  79. # A see-string always starts with *; we replace the * with ~
  80. # so that see references always come last.  (They are usually unique.)
  81.     if p == "*" then
  82.         return "~"
  83.     p := integer(p)
  84.     return (
  85.         if p < 0 then
  86.             "-" || right(-p, 4, "0")
  87.         else
  88.             right(p, 5, 0)
  89.         )
  90. end